package tkM

import (
	cp "db2s/componentInit"
	mq "db2s/topic-mq"
)
import "sync/atomic"

func subTaskInit(plan2 *SchedulePlan2) {
	var (
		selectCount      int64 = 0
		insertCount      int64 = 0
		recordCount      int64 = 0
		taskStatus       int64 = 1
		beginTime        int64 = 0
		outlierDetection int64 = 0
		ErrorInfo        atomic.Value
		partitionName    atomic.Value
		tableObject      string
	)
	ErrorInfo.Store("")
	tableObject = "N"
	plan2.subTaskInfo = &mq.SubTasks{
		Schema:           plan2.Object.Schema,
		Table:            plan2.Object.Table,
		Loc:              plan2.TableLocNumber,
		SAccumulate:      &selectCount,
		RecordCount:      &recordCount,
		DAccumulate:      &insertCount,
		TaskStatus:       &taskStatus,
		ErrorInfo:        ErrorInfo,
		PartitionName:    partitionName,
		BeginTime:        &beginTime,
		OutlierDetection: &outlierDetection,
		TableObject:      tableObject,
		TopicCount: func() (tc map[string]*int64) {
			tc = make(map[string]*int64)
			for _, v1 := range GetCheckToTopicMQListeners()[cp.CheckMode(GlobalPConfigs.rules.CheckMode)] {
				var ff int64 = 0
				tc[string(v1.TopicName)] = &ff
			}
			return
		}(),
	}
	plan2.subTaskInfo.IndexName.Store(func() string {
		if len(plan2.ReadOptimizer.IndexName) == 0 {
			return "no"
		}
		return plan2.ReadOptimizer.IndexName
	}())
	plan2.subTaskInfo.IndexCardinality.Store("0")
	plan2.subTaskInfo.IndexObject.Store("M")
	if PlanContext.TaskBarTable.SubTasks != nil {
		PlanContext.TaskBarSubsStatusSend(plan2.subTaskInfo)
	}
}
func (context *SchedulePlanContext) SubTaskBarAccumulate(n any) {
	context.TaskBarTable.BarAccumulate(n)
}
func (context *SchedulePlanContext) SpeedTaskBarAccumulate(n any) {
	context.TaskBarTable.SpeedAccumulate(n)
}
func (context *SchedulePlanContext) taskBarEndTopicAdd(n any) {
	context.TaskBarTable.TopicBarAdd(n)
}
func (context *SchedulePlanContext) TaskBarEndTopicDel(n any) {
	context.TaskBarTable.TopicBarDel(n)
}
func (context *SchedulePlanContext) TaskBarSubsStatusSend(table *mq.SubTasks) {
	context.TaskBarTable.BarSubsStatusSwapSend(table)
}
func (context *SchedulePlanContext) TaskBarSubsStatusSwap(table *mq.SubTasks, r string) {
	context.TaskBarTable.BarSubsStatusSwap(table, r)
}
func (context *SchedulePlanContext) TaskBarSubsValueSwap(table *mq.SubTasks, s, r string) {
	context.TaskBarTable.BarSubsValueSwap(table, s, r)
}
func (context *SchedulePlanContext) TaskBarSubsErrorSwap(table *mq.SubTasks, r string) {
	context.TaskBarTable.BarSubsErrorSwap(table, r)
}
func (context *SchedulePlanContext) TaskBarSubsStatusGet(table *mq.SubTasks) int64 {
	return context.TaskBarTable.BarSubsStatusGet(table)
}
func (context *SchedulePlanContext) TPodTableStatusSwap(status *int64, r string) {
	switch r {
	case "error":
		atomic.SwapInt64(status, -1)
	case "abnormal":
		atomic.SwapInt64(status, -2)
	case "finish":
		atomic.SwapInt64(status, 0)
	}
}
